<?php
/**
 * @author shutyuka@gmail.com
 * @modified 2011-02-20
 */
class AccessHistory{
	
	static public $FLAG_INACTIVE = 0;
	
	static public $FLAG_UNACTIVE = 10;
	
	static public $TABLE_NAME="access_history";

	static public function Create($userId, $log) {
		$preengage = array();
		$preengage['user_id'] = $userId;
		$preengage['log'] = $log;
		
		
		$preengage['create_time'] = time();
		$preengage['id'] = DB::Insert(self::$TABLE_NAME, $preengage);
		
		return $preengage['id'];
	}
	
	static public function ChangeObject($opUid, $preObj, $obj, $tableName){
		$log = 'change preObj<';
		foreach ($preObj as $k => $v){
			
			if( Utility::EndsWith($k, "time") ){
				$v = date("y-m-d H:i", $v);
			}
			$log = $log."$k => $v";
			if( $k == "staff_id" || $k == "designation_stuff" ){
				$temp = Staff::getById($v);
				$log = $log.'('.$temp['family_name'].' '.$temp['personal_name'].')';
			}
			if( $k == "service_id" ){
				$temp = Service::getById($v);
				$log = $log.'('.$temp['service_name'].')';
			}
			if( $k == "user_id" ){
				$temp = Users::GetUser($v);
				$log = $log.'('.$temp['family_name'].' '.$temp['personal_name'].')';
			}
			
			$log = $log.', ';
		}
		$log = $log.'> 2 obj <';
		foreach ($obj as $k => $v){
			if( Utility::EndsWith($k, "time") ){
				$v = date("y-m-d H:i", $v);
			}
			$log = $log."$k => $v";
			if( $k == "staff_id" || $k == "designation_stuff" ){
				$temp = Staff::getById($v);
				$log = $log.'('.$temp['family_name'].' '.$temp['personal_name'].')';
			}
			if( $k == "service_id" ){
				$temp = Service::getById($v);
				$log = $log.'('.$temp['service_name'].')';
			}
			if( $k == "user_id" ){
				$temp = Users::GetUser($v);
				$log = $log.'('.$temp['family_name'].' '.$temp['personal_name'].')';
			}
			
			$log = $log.', ';
		}
		
		$log = $log.'> in '.$tableName;
		
		self::Create($opUid, $log);
	}
	
	static public function DeleteObject($opUid, $obj, $tableName){
		$log = 'delete  obj <';
		foreach ($obj as $k => $v){
			if( Utility::EndsWith($k, "time") ){
				$v = date("y-m-d H:i", $v);
			}
			$log = $log."$k => $v";
			if( $k == "staff_id" || $k == "designation_stuff" ){
				$temp = Staff::getById($v);
				$log = $log.'('.$temp['family_name'].' '.$temp['personal_name'].')';
			}
			if( $k == "service_id" ){
				$temp = Service::getById($v);
				$log = $log.'('.$temp['service_name'].')';
			}
			if( $k == "user_id" ){
				$temp = Users::GetUser($v);
				$log = $log.'('.$temp['family_name'].' '.$temp['personal_name'].')';
			}
			
			$log = $log.', ';
		}
		$log = $log.'> in '.$tableName;
		
		self::Create($opUid, $log);
	}
	
	static public function AddObject($opUid, $obj, $tableName){
		$log = 'add  obj <';
		foreach ($obj as $k => $v){
			if( Utility::EndsWith($k, "time") ){
				$v = date("y-m-d H:i", $v);
			}
			
			$log = $log."$k => $v";
			if( $k == "staff_id" || $k == "designation_stuff" ){
				$temp = Staff::getById($v);
				$log = $log.'('.$temp['family_name'].' '.$temp['personal_name'].')';
			}
			if( $k == "service_id" ){
				$temp = Service::getById($v);
				$log = $log.'('.$temp['service_name'].')';
			}
			if( $k == "user_id" ){
				$temp = Users::GetUser($v);
				$log = $log.'('.$temp['family_name'].' '.$temp['personal_name'].')';
			}
			
			
			$log = $log.', ';
		}
		$log = $log.'> in '.$tableName;
		
		self::Create($opUid, $log);
	}
	
	
	
	static public function GetList($startYear, $startMonth, $startDay, $endDay){
		$orderStartTime = mktime(0, 0, 0, $startMonth, $startDay, $startYear);
		$orderEndTime = mktime(23, 59, 59, $startMonth, $endDay, $startYear);
		$oc = array( 
			"create_time >= $orderStartTime",
			"create_time <= $orderEndTime",
		);
		
		$all = DB::LimitQuery(self::$TABLE_NAME, array(
				'condition' => $oc,
				'order' => 'ORDER BY id DESC'
				));
		return $all;
		
	}
	
}
